//
//  CarViewController.m
//  AutoHome
//
//  Created by wqh on 15-5-9.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "CarViewController.h"
#import "TopTabControl.h"
#import "SYQRCodeViewController.h"
#import "QRResultViewController.h"

#import "BrandCar.h"


@interface CarViewController ()<TopTabControlDataSource>
{
    NSArray *_viewControllersArr;
}
@property (nonatomic) TopTabControl *tabControl;
@property (nonatomic) NSMutableArray *recommendInfo;

@end

@implementation CarViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self displayCustomTabBar];
//    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _viewControllersArr = @[@"BrandCar",@"ScreenCar"/*,@"DepreciateCar",@"SecondHandCar*/];
    
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    self.recommendInfo = [NSMutableArray arrayWithArray:@[@"品牌",@"筛选"/*,@"降价",@"找二手车"*/]];
    
    // Do any additional setup after loading the view, typically from a nib.
    self.tabControl = [[TopTabControl alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-49)];
    self.tabControl.datasource = self;
    [self.tabControl reloadData];
    self.tabControl.showIndicatorView = YES;
    [self.view addSubview:self.tabControl];
    
    [self createQRButton];
    
}

- (void)createQRButton {
    UIButton *qrButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [qrButton setBackgroundImage:[UIImage imageNamed:@"icon_qrcode"] forState:UIControlStateNormal];
    qrButton.frame = CGRectMake(kScreenWidth-50, 25, 40, 40);
    [qrButton addTarget:self action:@selector(qrButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:qrButton];
}

- (void)qrButtonClick:(UIButton *)button {
    SYQRCodeViewController *vc = [SYQRCodeViewController new];
    
    vc.SYQRCodeCancleBlock = ^(SYQRCodeViewController *vc) {
        [vc dismissViewControllerAnimated:YES completion:nil];
    };
    
    vc.SYQRCodeSuncessBlock = ^(SYQRCodeViewController *vc,NSString *qrString){
        [vc dismissViewControllerAnimated:NO completion:nil];
        
        QRResultViewController *resultView = [QRResultViewController new];
        resultView.qrStr = qrString;
        [self.navigationController pushViewController:resultView animated:YES];
    };
    
    [self presentViewController:vc animated:YES completion:nil];
}

//设置菜单的个数
- (NSInteger)TopTabMenuCount:(TopTabControl *)tabCtrl
{
    return self.recommendInfo.count;
}

- (CGFloat)TopTabHeight:(TopTabControl *)tabCtrl {
    return 44;
}

- (CGFloat)TopTabWidth:(TopTabControl *)tabCtrl {
    return 240;
}

- (TopTabMenuItem *)TopTabControl:(TopTabControl *)tabCtrl itemAtIndex:(NSUInteger)index {
    TopTabMenuItem *item = [[TopTabMenuItem alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:self.recommendInfo[index] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor customBlueColor] forState:UIControlStateSelected];
    button.frame = item.bounds;
    button.userInteractionEnabled = NO;
    
    [item addSubview:button];
    
    return item;
}

- (TopTabPage *)TopTabControl:(TopTabControl *)tabCtrl pageAtIndex:(NSUInteger)index {
    
    TopTabPage *page = [[TopTabPage alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-49-64)];
    page.backgroundColor = [UIColor greenColor];
    
    Class className = NSClassFromString(_viewControllersArr[index]);
    if (index  == 0) {
        BrandCar *vc = [[className alloc] init];
        
        vc.view.frame = page.bounds;
        
        vc.myPageJumpBlcok = ^ (UIViewController *vc) {
            [self.navigationController pushViewController:vc animated:YES];
        };
        
        vc.myHiddeTabBarBlock = ^ (BOOL isHidden){
            NSArray *array = self.tabBarController.view.subviews;
            for (UIView *view in array) {
                if (view.tag == 100) {
                    view.hidden = isHidden;
                }
            }
        };
        
        vc.brandCarView.myPicJumpBlock = ^(UIViewController *vc) {
            
            [self.navigationController pushViewController:vc animated:YES];
        };
        vc.brandCarView.myHideTabBar = ^ {
            NSArray *array = self.tabBarController.view.subviews;
            for (UIView *view in array) {
                if (view.tag == 100) {
                    view.hidden = YES;
                }
            }
        };
        
        page.vc = vc;
        [page addSubview:vc.view];
        return page;
    }
    BaseViewController *vc = [[className alloc] init];
    
    vc.view.frame = page.bounds;
    
    vc.myPageJumpBlcok = ^ (UIViewController *vc) {
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    vc.myHiddeTabBarBlock = ^ (BOOL isHidden){
        NSArray *array = self.tabBarController.view.subviews;
        for (UIView *view in array) {
            if (view.tag == 100) {
                view.hidden = isHidden;
            }
        }
    };
    
    page.vc = vc;
    [page addSubview:vc.view];
    
    
    return page;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
